﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Mdsd.Cpoe.Client.Login;
using Medicalsystem.Docare.ExceptionHandling;
using CPOE.Framework;

namespace Mdsd.Cpoe.Client
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
            Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);

            //登陆页
            LoginScreen login = new LoginScreen();
            //登陆事件
            login.LoginEvent += delegate(object sender, LoginEventArgs e)
            {
                if (e.RunApp)
                {
                    //主页的透明度设为零
                    var homepage = new HomePage();
                    homepage.Opacity = 0;
                    homepage.Load += delegate
                    {
                        var timer = new System.Windows.Forms.Timer();
                        timer.Interval = 100;
                        timer.Tick += delegate
                        {
                            //将主页的透明度设为100%,并关闭登陆界面
                            homepage.Opacity = 1;
                            login.Dispose();
                            timer.Stop();
                            timer.Dispose();
                        };
                        timer.Start();
                    };
                    //执行程序
                    Application.Run(homepage);
                }
                else
                {
                    //异常出错，退出系统
                    Application.Exit();

                }
            };
            login.ShowDialog();
        }
        /// <summary>
        /// 处理异常
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
            ExceptionManager.Handle(e.Exception);
        }
        /// <summary>
        /// 处理异常
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            var exception = e.ExceptionObject as Exception;
            ExceptionManager.Handle(exception);
        }

        
    }
}
